package com.bardpub.weblite.ui 
{
	import com.bardpub.weblite.connect.WebRequest;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.clearTimeout;
	import flash.utils.getDefinitionByName;
	import flash.utils.setTimeout;
	
	/**
	 * Copyright © 2010-2012, BardPub.com
	 * @fileName Page.as
	 * @author LittleBard
	 * @version 1.0
	 * @date 2010/5/5
	 * @descrition 
	 * @others
	 * @history
	 */
	
	public class Page extends MovieClip
	{
		public static const FADIN:String = "fadin";
		public static const FADOUT:String = "fadout";
		

		private var __parent:Page;
		private var __currentChild:Page;
		protected var __isfading:int = 0;
		private var __selfPage:PageInfo;
		private var __finalInfo:PageInfo;
		private var __isLoading:Boolean;
		private var __toload:Boolean = false;
		protected var __loadBar:BaseLoadbar;
		private var __timeOut:Number;
		protected var __loadBarRef:Class;
		

		/**
		 * 载入子页面
		 * @param	childName
		 */
		public function loadChild(info:PageInfo):void
		{
			if (__finalInfo != null && __finalInfo.contains(info))
			{
				return;
			}
			var child:PageInfo = getChildInfo(info, __selfPage);
			if (child != null)
			{
				__finalInfo = info;
			}
			else 
			{
				return;
			}
			
			if (__currentChild != null)
			{
				if (__currentChild.selfInfo.contains(child))
				{
					__currentChild.autoLoadChild(__finalInfo);
				} 
				else 
				{
					__toload = true;
					__currentChild.fadOut();
				}
				return;
			}
			
			tobeLoad();
		
		}
		
		/**
		 * 获取载入进度条,以便在本页面载入子页面的时候显示进度
		 */
		protected function getLoadBar():void 
		{
			if (__loadBar == null)
			{
				if (__loadBarRef != null)
				{
					__loadBar = new __loadBarRef() as BaseLoadbar;
					__loadBar.resize(stage.stageWidth, stage.stageHeight);
					
				}
			}
		}
		
		/**
		 * 现在载入进度条
		 */
		protected function showLoadBar():void 
		{	
			getLoadBar();
			if (__loadBar != null)
			{
				if (__loadBar.parent)
				{
					
				}
				else
				{
					addChild(__loadBar);
					__loadBar.visible = false;
					__timeOut = setTimeout(viewLoadbar, 200);
				}
				__loadBar.percent = 0;	
			}
		}
		
		/**
		 * 显示进度条
		 */
		private function viewLoadbar():void
		{
			if (__loadBar)
			{
				__loadBar.visible = true;
			}
		}
		
		/**
		 * 隐藏LoadBar
		 */
		protected function hideLoadBar():void 
		{
			clearTimeout(__timeOut);
			if (__loadBar && __loadBar.parent)
			{
				removeChild(__loadBar);
			}
		}
		
		/**
		 * 载入子页面
		 */
		protected function tobeLoad():void 
		{
			showLoadBar();
			beforeLoadChild();
			var child:PageInfo = getChildInfo(__finalInfo, __selfPage);
			if (child == null)
			{
				return;
			}
			__isLoading = true;
			
			WebRequest.getInstance().singleLoad(child.pageURL,
												WebRequest.TYPE_SWF,
												onLoadChildSuccess,
												onLoadChildProgress,
												onLoadChildFailed);	

			
		}
		
		/**
		 * 在载入子页面前触发
		 */
		protected function beforeLoadChild():void 
		{
			
		}
		
		/**
		 * 在子页面载入完成后触发
		 * @param	success  子页面是否载入成功
		 */
		protected function afterLoadChild(success:Boolean):void 
		{
			
		}
		
		/**
		 * 清除页面元素
		 * 声音，视频，正在下载的子页面等等
		 */
		public function shutDown():void 
		{
			
			__toload = false;
			if (__currentChild)
			{
				__currentChild.shutDown();
			}
			WebRequest.getInstance().cleanSingleLoad();
			
			__isLoading = false;
		}
		
		/**
		 * 重新设置页面坐标
		 */
		public function resize(stageWidth:Number, stageHeight:Number):void 
		{
			if (__currentChild != null)
			{
				__currentChild.resize(stageWidth, stageHeight);
			}
		}
		
		
		/**
		 * 渲染子页面
		 */
		protected function renderPage():void 
		{
			__toload = false;
			addChild(__currentChild);
			__currentChild.fadIn();
			__currentChild.addEventListener(Page.FADIN, onChildFadIn);
			__currentChild.addEventListener(Page.FADOUT, onChildFadOut);
			__currentChild.selfInfo = getChildInfo(__finalInfo, __selfPage);
		}
		
		/**
		 * 子页面进场动画完成后触发
		 * @param	event	
		 */
		protected function onChildFadIn(event:Event):void 
		{
			__currentChild.autoLoadChild(__finalInfo);
		}
		
		/**
		 * 子页面退场完成后触发
		 * @param	event	
		 */
		protected function onChildFadOut(event:Event):void 
		{
			
			if (__toload)
			{
				cleanChild();
				tobeLoad();
			}
			__toload = false;
		}
		
		/**
		 * 载入子页面成功后发生
		 * @param	data
		 */
		protected function onLoadChildSuccess(data:*):void 
		{
			afterLoadChild(true);
			__isLoading = false;
			cleanChild();
			__currentChild = Page(data);
			renderPage();
			hideLoadBar();
		}
		
		/**
		 * 清除子元素
		 * @return
		 */
		protected function cleanChild():Boolean 
		{
			if (__currentChild != null)
			{
				__currentChild.shutDown();
				__currentChild.removeEventListener(Page.FADIN, onChildFadIn);
				__currentChild.removeEventListener(Page.FADOUT, onChildFadOut);
				if (__currentChild.parent)
				{
					removeChild(__currentChild);
				}
				__currentChild = null;
			}
			
			return true;
		}
		
		/**
		 * 载入子页面进度变化时发生
		 * @param	bytesLoaded
		 * @param	bytesTotal
		 */
		protected function onLoadChildProgress(bytesLoaded:Number, bytesTotal:Number):void 
		{
			if (__loadBar)
			{
				__loadBar.percent =  Math.floor(bytesLoaded / bytesTotal * 100);
			}
		}
		
		/**
		 * 当子页面载入失败时发生
		 */
		protected function onLoadChildFailed():void 
		{
			hideLoadBar();
			afterLoadChild(false);
			__isLoading = false;
			var info:PageInfo = getChildInfo(__finalInfo, __selfPage);
			var msg:String = "Load Page:" + info.pageName +
							 " src(" + info.pageURL + ")" +
							 " failed";
			throw new Error(msg);
		}
		
		
		/**
		 * 自动载入子页面
		 * @param	pageInfo
		 */
		public function autoLoadChild(pageInfo:PageInfo):void 
		{
			if (pageInfo == null)
			{
				return;
			}
			
			if (pageInfo.contains(__selfPage))
			{
				return;
			} 
			
			//此处需要修改，loadChild之前需判断pageInfo是否为当前页面的子页面
			//WebConfig.isChildPage(parent, child);
			if (this.selfInfo.depth > pageInfo.depth)
			{
				loadChild(pageInfo);
			}
			
			
			
			
		}
		
		/**
		 * 获取子页面的 PageInfo
		 * @param	finfo	最终页面PageInfo实例
		 * @param	self	自身PageInfo实例
		 * @return
		 */
		private function getChildInfo(finfo:PageInfo, self:PageInfo):PageInfo 
		{
			var info:PageInfo = finfo;
			while (info.parent && !info.parent.contains(self))
			{
				info = info.parent;
			}
			if (info.contains(self))
			{
				return null;
			}
			else 
			{
				return info;
			}
			
		}
		
		/**
		 * 创建 com.bardpub.weblite.ui.Page 类的新实例
		 */
		public function Page() 
		{
			if (stage)
			{
				initialize();
				resize(stage.stageWidth, stage.stageHeight);
				fadIn();
			} 
			else 
			{
				addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			}
		}
		
		/**
		 * 初始化
		 */
		protected function initialize():void 
		{
			try
			{
				__loadBarRef = this.loaderInfo.applicationDomain.getDefinition("SelfLoad") as Class;
			}
			catch (e:*)
			{
				
			}
			__isLoading = false;
			
			
		}
		
		/**
		 * 当被显示在舞台时发生
		 * @param	e
		 */
		private function onAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			initialize();
			resize(stage.stageWidth, stage.stageHeight);
		}
		
		/**
		 * 渐显动画
		 */
		public function fadIn():void 
		{
			__isfading = 1;
		}
		
		/**
		 * 消失动画
		 */
		public function fadOut():void 
		{
			__isfading = 2;
			if (__currentChild)
			{
				__currentChild.fadOut();
				__currentChild.removeEventListener(Page.FADIN, onChildFadIn);
				__currentChild.removeEventListener(Page.FADOUT, onChildFadOut);
				
			}
			
			shutDown();
		}
		
		/**
		 * 渐显动画完成时触发
		 */
		protected function onFadInComplete():void 
		{
			__isfading = 0;
		
			dispatchEvent(new Event(FADIN));
			
		}
		
		/**
		 * 消失动画完成时触发
		 */
		protected function onFadOutComplete():void 
		{
			__isfading = 0;
			dispatchEvent(new Event(FADOUT));
			this.visible = false;
		}
		
		/**
		 * 获取/设置 本实例的PageInfo
		 */
		public function set selfInfo(info:PageInfo):void
		{
			__selfPage = info;
		}
		public function get selfInfo():PageInfo
		{
			return __selfPage;
		}
		
	}

}